/*
 * @Author: wuzhen
 * @Date: 2020-01-19 14:54:51
 * @LastEditors: wuzhen
 * @LastEditTime: 2020-04-18 14:04:54
 * @Description: 全局方法
 */
import { formatLetter } from "@/libs/units";
import { mapGetters, mapMutations } from "vuex";
import { getGroupMember, getUserInfo } from "@/request/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters([
      "jsessionId",
      "unf",
      "screen",
      "online",
      "userInfo",
      "friends",
      "groups",
      "chatting",
      "chatUsers",
      "chatMsg",
      "input_httpSeqno"
    ])
  },
  methods: {
    ...mapMutations([
      "set_jsessionId",
      "set_unf",
      "set_groups",
      "set_friends",
      "set_chatting",
      "set_chatUsers",
      "set_chatMsg",
      "set_input_httpSeqno"
    ]),
    // 修改好友
    // storeFriends({
    //   name = "",
    //   id = null,
    //   face = "",
    //   letter = "",
    //   birthday = "",
    //   gender = null,
    //   onlinestatus=10,
    //   msgkind=20
    // }) {
    //   // 判断是否存在
    //   const currentIndex = this.friends.findIndex(v => v.id === id);
    //   if (~currentIndex < 0) {
    //     this.friends.splice(currentIndex, 1);
    //   }
    //   this.friends.push({
    //     name,
    //     id,
    //     face,
    //     letter,
    //     birthday,
    //     gender,
    //     onlinestatus,
    //     msgkind
    //   });
    //   this.set_friends(this.friends);
    // },
    // 修改正在聊天中成员
    storeChatting({ msgkind = null, id = null, status = null }) {
      this.set_chatting({ msgkind, id, status });
    },
    // 修改正在聊天的好友
    storeChatUsers({
      msgkind = null,
      id = null,
      msgStatus = 0,
      total = 0,
      messageid = null,
      content = {},
      msgtype = null,
      createtime = null
    }) {
      // 判断是否存在
      const obj = {
        id,
        total,
        msgStatus,
        msgkind,
        messageid,
        content,
        msgtype,
        createtime
      };
      console.log(obj);
      const currentIndex = this.chatUsers.findIndex(v => v.id === id);
      if (~currentIndex < 0) {
        const currentUser = this.chatUsers[currentIndex];
        obj["total"] = currentUser["total"] + 1;
        this.chatUsers.splice(currentIndex, 1);
      } else if (this.chatting.id !== id) {
        obj["total"] = 1;
      }
      this.chatUsers.unshift(obj);
      this.set_chatUsers(this.chatUsers);
    },
    // 获取用户详情
    async getUserInfoData(id) {
      try {
        const res = await getUserInfo({ id: id });
        const obj = {
          name: res.username,
          username: res.username,
          id: res.userid,
          face: res.face,
          letter: formatLetter(res.username),
          msgkind: 20,
          mobile: res.mobile,
          personlsign: res.personlsign || ""
        };
        const currentIndex = this.friends.findIndex(v => v.id === id);
        if (~currentIndex < 0) {
          this.friends.splice(currentIndex, 1, {
            ...obj,
            ...{
              name: this.friends[currentIndex].name,
              secretstatus: this.friends[currentIndex].secretstatus,
              secret: this.friends[currentIndex].secret || ""
            }
          });
        } else {
          this.friends.push(obj);
        }
        console.log(this.friends);
        this.set_friends(this.friends);
      } catch (error) {
        throw error;
      }
    },
    // 获取群成员信息
    async getGroupMemberData(id) {
      try {
        const res = await getGroupMember({ groupId: id });
        // 存储群组成员信息
        const groupMember = res.result.userGroups;
        const groupDetail = res.result.groupDetail;
        const obj = {
          grouptype: groupDetail.grouptype,
          usernum: groupDetail.usernum,
          name: groupDetail.groupname,
          id: groupDetail.groupid,
          face: groupDetail.groupface,
          letter: "群聊",
          msgkind: 30,
          createuserid: groupDetail.createuserid,
          syssetting: groupDetail.syssetting, // 1是开启禁言，0是关闭禁言
          groupMember: groupMember
        };
        const currentIndex = this.groups.findIndex(v => v.id === id);
        if (~currentIndex < 0) {
          this.groups.splice(currentIndex, 1, {
            ...obj
          });
        } else {
          this.groups.push(obj);
        }
        this.set_groups(this.groups);
      } catch (error) {
        throw error;
      }
    }
  }
};
